# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::storage::IncrementalDB;
use strict;
use loader::IncrementalDB;
use saver::IncrementalDB;
use core::Empire;
use Error;
use DBI;

sub new
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self = {};
	$self->{ConnectString} = shift;
	$self->{DB} = DBI->connect("DBI:".$self->{ConnectString}, "", "", {'RaiseError' => 1, 'unicode' => 1});
	$self->{Id} = shift;
	bless ($self, $class);
	return $self;
}

sub GetLast
{
	my $self = shift;
	my $empire = dcExplorer::core::Empire->new($self->{Id});
	my $sourceBuilder = dcExplorer::loader::IncrementalDB->new($empire);
	$sourceBuilder->loadDB($self->{DB});
	return $empire;
}

sub GetTurn
{
	my $self = shift;
	my $turn = shift;
	my $empire = dcExplorer::core::Empire->new($self->{Id});
	my $sourceBuilder = dcExplorer::loader::IncrementalDB->new($empire);
	$sourceBuilder->loadDB($self->{DB}, $turn);
	return $empire;
}

sub Save
{
	my $self = shift;
	my $sourceEmpire = $self->GetLast();
	my $updateEmpire = shift;
	my $saver = dcExplorer::saver::IncrementalDB->new($self->{DB}, $sourceEmpire);
	$saver->saveEmpire($updateEmpire);
}

1;
